package net.esj.basic.utils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {

	public static Date now(){
		return new Date();
	}

	public static int calcMonthsBetween(Date date1, Date date2) {
		int monsub = Math.abs(date1.getMonth() - date2.getMonth());
		int yearsub = Math.abs(date1.getYear() - date2.getYear());
		int v = Math.abs(yearsub * 12 + monsub);
		return v;
	}

	public static int calcDaysBetween(Date date1, Date date2) {
		long timemill = Math.abs(date1.getTime() - date2.getTime());
		int day = (int) (timemill / (1000 * 60 * 60 * 24));
		return day;
	}

	/**
	 * 精确计算年龄
	 * 
	 * @param birthday
	 *            出生日期
	 * @return
	 */
	public static int calcAxactAge(Date birthday) {
		Calendar birth = Calendar.getInstance();
		Calendar now = Calendar.getInstance();
		birth.setTime(birthday);
		birth.set(Calendar.YEAR, now.get(Calendar.YEAR));
		int age = 0;
		if (birth.after(now)) {// 如果日期在当前之后，说明还没过生日，减一岁
			birth.setTime(birthday);
			age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR) - 1;
		} else {
			birth.setTime(birthday);
			age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
		}
		return age;
	}

	public static Date getDateByYearDelay(int age, boolean isEnd) {
		Calendar c = Calendar.getInstance();
		int y = c.get(Calendar.YEAR);
		y -= age;
		if (y <= 0) {
			return new Date();
		}
		c.set(Calendar.YEAR, y);
		if (isEnd) {
			c.set(y, 12, 31, 23, 59, 59);
		} else {
			c.set(y, 1, 1, 0, 0, 0);
		}
		return c.getTime();
	}

	@SuppressWarnings("deprecation")
	public static boolean checkGreaterMonth(Date date, int month) {
		int now = (new Date()).getMonth();
		int then = date.getMonth();
		long dateSub = now - then;
		if (dateSub >= month) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断当前时间与date间的间隔天数是否超过day（严格按1天为24小时）
	 * 
	 * @param date
	 * @param day
	 * @return true:没有超过 false:超过
	 */
	public static boolean checkLessDay(Date date, int day) {
		long now = (new Date()).getTime();
		long then = date.getTime();
		long dateSub = then - now;
		long sub = Math.abs(dateSub*1l / (1000 * 60 * 60 * 24));
		if (sub <= day) {
			return true;
		} else {
			return false;
		}
	}
	/**
	 * 不严格的判断当前时间与date间的天数差是否小于指定day
	 * @param date
	 * @param day
	 * @return
	 */
	public static boolean checkLessDayLooseLy(Date date, int day){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		int now = calendar.get(Calendar.DAY_OF_YEAR);
		calendar.setTime(date);
		int then = calendar.get(Calendar.DAY_OF_YEAR);
		int dateSub = Math.abs(then-now);
		if (dateSub <= day) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获得这天的0点0分0秒
	 * 
	 * @param date
	 * @return
	 */
	public static Date getBeginOfDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 获得这天的23点59分59秒
	 * 
	 * @param date
	 * @return
	 */
	public static Date getEndOfDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}
	
	/**
	 * 获取几天后的指定时间
	 * @param date
	 * @param count
	 * @param type 1为早上，2为晚上的时间点
	 * @return
	 */
	public static Date getFutureDate(Date date , int count , int type){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_YEAR, count);
		if(type==1){//一天的早上
			return getBeginOfDay(c.getTime());
		}else{//一天的结束
			return getEndOfDay(c.getTime());
		}
	}

	/**
	 * 
	 * @param date
	 *            初始时间
	 * @param field
	 *            增加key
	 * @param amount
	 *            增加值
	 * @return
	 */
	public static Date generateDate(Date date, int field, int amount) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(field, amount);
		return calendar.getTime();
	}

	public static boolean beforeNow(Date date) {
		return Calendar.getInstance().getTime().after(date);
	}

	public static boolean afterNow(Date date) {
		return Calendar.getInstance().getTime().before(date);
	}

	public static boolean isInOneDay(long mills1, long mills2) {
		Calendar cal = Calendar.getInstance();
		int y1, y2, m1, m2, d1, d2;
		cal.setTimeInMillis(mills1);
		y1 = cal.get(Calendar.YEAR);
		m1 = cal.get(Calendar.MONTH);
		d1 = cal.get(Calendar.DAY_OF_YEAR);

		cal.setTimeInMillis(mills2);
		y2 = cal.get(Calendar.YEAR);
		m2 = cal.get(Calendar.MONTH);
		d2 = cal.get(Calendar.DAY_OF_YEAR);

		if (y1 == y2 && m1 == m2 && d1 == d2) {
			return true;
		}
		return false;
	}

	/**
	 * 获得截止日期
	 * 在当前日期的amount个field后的日期再减一天
	 * eg:
	 * field = Calendar.MONTH;
	 * amount = 3;
	 * 当前日期 2012-06-15
	 * fanh： 2012-09-14
	 * @param field 时间类型 month year ...
	 * @param amount 数量
	 * @return
	 */
	public static String getFinishDate(int field, int amount) {
		// 当前日期和前一天
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				DateFormat.DEFAULT_DATETIME_FORMAT_SEC);

		Calendar rightNow = Calendar.getInstance();
		int intField = 0;

		rightNow.add(intField, field);
		rightNow.add(Calendar.DATE, -1);
		String day = formatter.format(rightNow.getTime());
		return day;
	}
	
	/**
	 * 获取当前星期名称
	 */
	public static String getNameOfWeek(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(getBeginOfDay(date));
		int w = cal.get(Calendar.DAY_OF_WEEK)-1;
		//0:星期天;1:星期一;2:星期二;3:星期三;4:星期四;5:星期五;6:星期六;
		if(w<0){
			w = 0;
		}
		switch (w) {
		case 0:
			return "星期天";
		case 1:
			return "星期一";
		case 2:
			return "星期二";
		case 3:
			return "星期三";
		case 4:
			return "星期四";
		case 5:
			return "星期五";
		case 6:
			return "星期六";
		default:
			return "星期天";
		}
	}
	
	/**
	 * 获取当前星期名称
	 */
	public static String getEnNameOfWeek(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(getBeginOfDay(date));
		int w = cal.get(Calendar.DAY_OF_WEEK)-1;
		//0:星期天;1:星期一;2:星期二;3:星期三;4:星期四;5:星期五;6:星期六;
		if(w<0){
			w = 0;
		}
		switch (w) {
		case 0:
			return "Sunday";
		case 1:
			return "Monday";
		case 2:
			return "Tuesday";
		case 3:
			return "Wednesday";
		case 4:
			return "Thursday";
		case 5:
			return "Friday";
		case 6:
			return "Saturday";
		default:
			return "星期天";
		}
	}
	
	/**
	 * 判断是否是周六或周日
	 * @param date
	 * @return
	 */
	public static boolean isFreeDay(Date date){
		Calendar cal = Calendar.getInstance();
		List<Date> list = new ArrayList<Date>();
		cal.setTime(getBeginOfDay(date));
		int w = cal.get(Calendar.DAY_OF_WEEK)-1;
		//0:星期天;1:星期一;2:星期二;3:星期三;4:星期四;5:星期五;6:星期六;
		if(w<0){
			w = 0;
		}
		switch (w) {
		case 0:
			return true;
		case 6:
			return true;
		default:
			return false;
		}
	}
	
	/**
	 * 获取当前时间下,如果今天为周5，6,7则显示下周周1至周5时间，否则返回之后到本周5的时间
	 * @param date
	 * @return
	 */
	public static List<Date> getLastWeekDates(Date date){
		Calendar cal = Calendar.getInstance();
		List<Date> list = new ArrayList<Date>();
		cal.setTime(getBeginOfDay(date));
		int w = cal.get(Calendar.DAY_OF_WEEK)-1;
		//0:星期天;1:星期一;2:星期二;3:星期三;4:星期四;5:星期五;6:星期六;
		if(w<0){
			w = 0;
		}
		if(w==0||w==5||w==6){//返回
			int amount = 0;
			switch (w) {
			case 0:
				break;
			case 5:
				amount = 2;
				break;
			case 6:
				amount = 1;
				break;
			}
			for(int i = 1 ; i < 6 ; i++){
				list.add(generateDate(cal.getTime(), Calendar.DAY_OF_YEAR, amount+i));
			}
		}else{
			int j = 1;
			for(int i = (w+1) ; i < 6 ; i++){
				list.add(generateDate(cal.getTime(), Calendar.DAY_OF_YEAR, j));
				j++;
			}
		}
		return list;
	}
	/**
	 * 用于计算两个日期相隔的天数,
	 */
	public static int calcDaysBetweenTwoDate(Date date1, Date date2){
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(date1);
		c2.setTime(date2);
		return Math.abs(c1.get(Calendar.DAY_OF_YEAR)-c2.get(Calendar.DAY_OF_YEAR));
	}
	
	/**
	 * 获取当前时间秒数的str01为1秒
	 */
	public static String getSecondOfThisTime(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int w = c.get(Calendar.SECOND);
		if(w<10){
			return "0" + w;
		}else{
			return w + "";
		}
	}
	
	/**
	 * 获取当前星期首字母
	 */
	public static String getFirstWordOfThisWeek(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int w = c.get(Calendar.DAY_OF_WEEK)-1;
		//0:星期天;1:星期一;2:星期二;3:星期三;4:星期四;5:星期五;6:星期六;
		switch (w) {
		case 0:
			return "S";
		case 1:
			return "M";
		case 2:
			return "T";
		case 3:
			return "W";
		case 4:
			return "T";
		case 5:
			return "F";
		case 6:
			return "S";
		default:
			return "S";
		}
	}
	
	/**
	 * 获取当前月份首字母
	 */
	public static String getFirstWordOfThisMonth(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int w = c.get(Calendar.MONTH);
		switch (w) {
		case 0:
			return "J";
		case 1:
			return "F";
		case 2:
			return "M";
		case 3:
			return "A";
		case 4:
			return "M";
		case 5:
			return "J";
		case 6:
			return "J";
		case 7:
			return "A";
		case 8:
			return "S";
		case 9:
			return "O";
		case 10:
			return "N";
		case 11:
			return "D";
		default:
			return "J";
		}
	}
	
	public static void main(String[] args) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		Date date = calendar.getTime();
		calendar.add(Calendar.DAY_OF_YEAR, 2);
		Date date2 = calendar.getTime();
		calendar.add(Calendar.DAY_OF_YEAR, -5);
		Date date3 = calendar.getTime();
		System.out.println(DateFormat.formateDateTime(date3, DateFormat.DEFAULT_DATE_FORMAT) + " " + beforeNow(date3));
		System.out.println(DateFormat.formateDateTime(date, DateFormat.DEFAULT_DATE_FORMAT) + " " + beforeNow(date));
		System.out.println(DateFormat.formateDateTime(date2, DateFormat.DEFAULT_DATE_FORMAT) + " " + beforeNow(date2));
	}
}
